class Solution:
    def minSumOfLengths(self, arr: list, target: int) -> int:
        prefSum = {0: -1}
        arrSum = 0
        for i in range(len(arr)):
            arrSum += arr[i]
            prefSum[arrSum] = i
        ans = 2**31
        prefSize = 2**31
        arrSum = 0
        for i in range(len(arr)):
            arrSum += arr[i]
            if arrSum - target in prefSum:
                prefSize = min(prefSize, i - prefSum[arrSum - target])
            if arrSum + target in prefSum and prefSize != 2**31:
                sufSize = prefSum[arrSum + target] - i
                ans = min(ans, sufSize + prefSize)

        return -1 if ans == 2**31 else ans
